package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.websocket.server.PathParam;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CodeRecord;
import com.ruoyi.system.domain.HatchetAgent;
import com.ruoyi.system.domain.HatchetMerchant;
import com.ruoyi.system.service.ICodeRecordService;
import com.ruoyi.system.service.IHatchetAgentService;
import com.ruoyi.system.service.IHatchetMerchantService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * 升级码交易记录 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-04-08
 */
@Controller
@RequestMapping("/system/codeRecord")
public class CodeRecordController extends BaseController {
	private String prefix = "system/codeRecord";
	@Autowired
	private ICodeRecordService codeRecordService;
	@Autowired
	private IHatchetAgentService agentService;
	@Autowired
	private IHatchetMerchantService merchantService;
	@RequiresPermissions("system:codeRecord:view")
	@GetMapping()
	public String codeRecord() {
		return prefix + "/codeRecord";
	}

	/**
	 * 查询升级码交易记录列表
	 */
	@RequiresPermissions("system:codeRecord:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(CodeRecord codeRecord) {
		//判断代理商编号
		if (StringUtils.isNotEmpty(codeRecord.getAgentId())) {
			HatchetAgent agent = agentService.selectHatchetAgentByNo(codeRecord.getAgentId());
			if (StringUtils.isNotNull(agent)) {
				codeRecord.setAgentId(agent.getID());
			}else {
				return getDataTable(new ArrayList<>());
			}
		}
		//判断用户编号
		if (StringUtils.isNotEmpty(codeRecord.getMerchantId())) {
			HatchetMerchant merchant = merchantService.selectHatchetMerchantByNo(codeRecord.getMerchantId());
			if (StringUtils.isNotNull(merchant)) {
				codeRecord.setMerchantId(merchant.getID());
			}else {
				return getDataTable(new ArrayList<>());
			}
		}
		startPage();
		List<CodeRecord> list = codeRecordService.selectCodeRecordList(codeRecord);
		//查询总交易码数量
		if (StringUtils.isNotEmpty(list)) {
			Long selectSumCount = codeRecordService.selectSumCount(codeRecord);
			list.get(0).setSumCount(selectSumCount);
		}
		return getDataTable(list);
	}

	/**
	 * 导出升级码交易记录列表
	 */
	@RequiresPermissions("system:codeRecord:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(CodeRecord codeRecord) {
		List<CodeRecord> list = codeRecordService.selectCodeRecordList(codeRecord);
		ExcelUtil<CodeRecord> util = new ExcelUtil<CodeRecord>(CodeRecord.class);
		return util.exportExcel(list, "codeRecord");
	}

	/**
	 * 新增升级码交易记录
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 给代理商添加升级码
	 */
	@RequiresPermissions("system:codeRecord:add")
	@Log(title = "升级码交易记录", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Map<String,Object> addSave(CodeRecord codeRecord,@PathParam("isCompany")String isCompany) {
		return codeRecordService.addcode(codeRecord,isCompany);
	}

	/**
	 * 修改升级码交易记录
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap) {
		CodeRecord codeRecord = codeRecordService.selectCodeRecordById(id);
		mmap.put("codeRecord", codeRecord);
		return prefix + "/edit";
	}

	/**
	 * 修改保存升级码交易记录
	 */
	@RequiresPermissions("system:codeRecord:edit")
	@Log(title = "升级码交易记录", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(CodeRecord codeRecord) {
		return toAjax(codeRecordService.updateCodeRecord(codeRecord));
	}

	/**
	 * 删除升级码交易记录
	 */
	@RequiresPermissions("system:codeRecord:remove")
	@Log(title = "升级码交易记录", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(codeRecordService.deleteCodeRecordByIds(ids));
	}

	/**
	 * 详情
	 * @param 查看的代理商升级码记录id
	 * @param mmap
	 * @return
	 */
	@RequiresPermissions("system:codeRecord:detail")
	@GetMapping("/detail/{id}")
	public String toDetail(@PathVariable("id") String id, ModelMap mmap) {
		mmap.put("codeRecord", codeRecordService.selectCodeRecordDetail(id));
		return prefix + "/detail";
	}

	
}
